<!DOCTYPE html>
<html>
  <head>
  	<div th:include="base-libs :: base-libs" />
    <title>更新已分配任务</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <style>
    	#north{
			height:47px;
			line-height:47px;
			font-size:16px;
			padding-left:35px;
			overflow:hidden;
			border:0;
		}
    	#fixedBox{
			text-align:right;
			overflow:hidden;
			border:0;
			padding-right:30px;
			border-top:1px solid #ddd;
		}
		#cancel{
			border: 1px solid #dedede;
		    background-color: #f1f1f1;
		    color: #333;
		}
		.laydate_body .laydate_box{
			left:77.4% !important;
		}
    </style>
    
    <script>
    	$(function(){
    		initView();
    		initData();
    	})
    	
    	function initData(){
    		var updateData = StoreCache.removeCache("updateData");
    		$("#updateTotal").html(updateData.length);
    		$.QuickRemote.AjaxJson(contextRoot + "wdqyhz/queryDfwjm", function(result){
    			dfwryObj = result;
	    		$("#updateTable").datagrid('loadData',updateData);
    		}, {'yfpfwArr':JSON.stringify(updateData)});
    	}
    	
    	function initView(){
    		//扩展datagrid编辑器类型（添加laydate时间控件）
			$.extend($.fn.datagrid.defaults.editors, {
				laydate: {
					init: function(container, options){
						var input = $('<input type="text" onclick="laydate()" class="laydate-icon" style="width:80%;border-radius:0">').appendTo(container);
						return input;
					},
					getValue: function(target){
						return $(target).val();
					},
					setValue: function(target, value){
						$(target).val(value);
					}
				}
			});
    		
    		$("#updateTable").datagrid({
				idField : "FWID",
				striped : true,
				clickToSelect: true,
				fitColumns: true,
				fit:true,
				border:true,
				scrollbarSize:0,
				singleSelect:true,
				rownumbers:true,
				columns : [[
					{field : "XM",title : "签约居民",width:40, align:'center'},
					{field : "FWMC",title : "服务项",width:60, align:'center'},
					{field : "FWNR",title : "服务项内容",width:120, align:'center'},
					{field : "QYTD",title : "新增服务(次数)",width:60, align:'center',formatter:function(){
						return 1;
					}},
					{field : "SFZH",title : "服务对象",width:60, align:'center',
						formatter: function(value,row){
							if(!row.dfwry){
								//先直接把人员数据保存，避免第一次选择后数据丢失
								row.dfwry = [{'SFZH':row.ZJHM,'XM':row.XM}].concat(dfwryObj[row.JMXXID]);
							}
							var dfwry = row.dfwry;
							$.each(dfwry, function(i,item){
								if(item.SFZH==value){
									value = item.XM;
								}
							})
							return value;
						},
						editor:{
                        type: "combobox",
                            options:{
								valueField:'SFZH',
								textField:'XM',
								editable:false
							}
    					}
					},
					{field : "USERID",title : "服务人员",width:60, align:'center',
						formatter: function(value,row,index){
							var tdryData = row.tdryData;
							$.each(tdryData, function(i,item){
								if(item.USERID==value){
									value = item.XM;
								}
							})
							return value;
						},
						editor:{
	                        type: "combobox",
                            options:{
								valueField:'USERID',
								textField:'XM',
								editable: false
							}
    					}	
					},
					{field : "YQFWSJ",title : "预期服务时间",width:60, align:'center',
						editor:{
	                        type: "laydate",
	                        editable: false,
    					}, formatter:function(value,row){
    						if(!StringUtils.isEmpty(value)){
    							row.YQFWSJ = value.substring(0,10);
    						}
    						return row.YQFWSJ;
    					}
					},
				]],
				method : "post",
				onClickRow : function(rowIndex, rowData){
					var editRowNum = $(this).data("editRowNum");
					$(this).data("editRowNum",rowIndex);
					if(!StringUtils.isEmpty(editRowNum) && rowIndex != editRowNum){
						$('#updateTable').datagrid('endEdit', editRowNum);
					}
					
					//1.开启编辑模式
					$('#updateTable').datagrid('beginEdit', rowIndex);
					//2.服务人员下拉框选项不存在则赋值
					var ed = $('#updateTable').datagrid('getEditor', {index:rowIndex,field:'USERID'});
					$(ed.target).combobox({
						valueField: "USERID",
					    textField: "XM",
					    data: rowData.tdryData
					});
					$(ed.target).combobox("setValues", rowData.USERID);//等同于默认选中

					//服务对象下拉框
					var ed1 = $('#updateTable').datagrid('getEditor', {index:rowIndex,field:'SFZH'}); 
					
					var dfwry = new Array(rowData.dfwry[0]);
					if(rowData.FWBSX==2){
						dfwry = rowData.dfwry;
					}
					$(ed1.target).combobox({
						valueField: "SFZH",
					    textField: "XM",
					    data: dfwry
					});
					if(rowData.SFZH) $(ed1.target).combobox("setValues", rowData.SFZH);
				},
			}); 
    	}
    	
    	/** 确认修改  */
    	function confirmUpdate(){
    		var editRowNum = $("#updateTable").data('editRowNum');
			$('#updateTable').datagrid('endEdit', editRowNum);
			var data = $("#updateTable").datagrid("getRows");
			//验证--因为数据可以手动删除
			var errMsg,errNum=0;
			var fpfwArr = [];
			$.each(data, function(i, item){
				var SYFWCS = parseInt(item.FWPC || 0)-parseInt(item.YFPCS || 0);
				
				errNum = i+1;
				if(StringUtils.isEmpty(item.USERID)){ 
					errMsg = '服务人员';
					return false;
				}else if(StringUtils.isEmpty(item.YQFWSJ)){
					errMsg = '预期服务时间';
					return false;
				}else if(StringUtils.isEmpty(item.SFZH)){
					errMsg = '服务对象';
					return false;
				}
				fpfwArr.push({
					FPFWID: item.FPFWID,
					USERID: item.USERID,
					YQFWSJ: item.YQFWSJ,
					SFZH: item.SFZH
				})
			})
			if(StringUtils.isEmpty(errMsg)){
				$.QuickRemote.AjaxJson(contextRoot + "wdqyhz/save", function(result){
					if(result.state){
						$.QuickAlert.alertNormal(result.info.success,{callback: function(){
							StoreCache.setCache("refresh",true);
							parent.layer.closeAll();
						}});
					}else{
						var info = result["info"];
						$.QuickAlert.alertFail({content: info["faultInfo"]});
					}
				}, {"fpfwArr":JSON.stringify(data), "method":"update"});
			}else{
				errMsg = '您的第<span style="color:red">'+errNum+'</span>数据的<span style="color:red">' + errMsg + '</span>还没有填写，不能提交！';
				$.QuickAlert.alertFail({content: errMsg});
			}
    	}
    	
    	/** 取消  */
		function cancel(){
			parent.layer.closeAll();
		}
    </script>

  </head>
  
  <body class="easyui-layout" fit="true" style="overflow:hidden;">
    	<div region="north" id="north" style>
			您已选中<span style="color:red" id="updateTotal"></span>条记录
		</div>
		<div region="center" style="height:400px;overflow:auto" border="false">
			<table id="updateTable">
			</table>
		</div>
		<div region="south" id="fixedBox" class="layui-layer-btn" style="height:50px;line-height:50px;">
			<button class="layui-layer-btn0" type="button" onclick="confirmUpdate()">确认</button>
			<button id="cancel" class="layui-layer-btn1" type="button" onclick="cancel()">取消</button>
		</div>
  </body>
</html>
